<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: ，开发者QQ群：50304283
// +----------------------------------------------------------------------

namespace app\system\admin;

use Env;
use hisi\Dir;
use think\Db;
/**
 * 后台默认首页控制器
 * @package app\system\admin
 */

class Index extends Admin
{
    /**
     * 首页
     * @author
     * @return mixed
     */
    public function index()
    {


        $admin_type = session('admin_type');
        $this->assign('admin_type',$admin_type);
        $sub0 = Db::name('subscribe')->where('status',0)->where('shop_id',session('shop_id'))->count('id');
        $sub1 = Db::name('subscribe')->where('status',1)->where('shop_id',session('shop_id'))->count('id');
        $sub2 = Db::name('subscribe')->where('status',2)->where('shop_id',session('shop_id'))->count('id');
        $sub3 = Db::name('subscribe')->where('status',3)->where('shop_id',session('shop_id'))->count('id');
        $sub5 = Db::name('subscribe')->where('status',5)->where('shop_id',session('shop_id'))->count('id');
        $sub6 = Db::name('subscribe')->where('status',6)->where('shop_id',session('shop_id'))->count('id');
        $sub7 = Db::name('product_order')->where('status',3)->where('shop_id',session('shop_id'))->count('id');
        $this->assign('sub0', $sub0);
        $this->assign('sub1', $sub1);
        $this->assign('sub2', $sub2);
        $this->assign('sub3', $sub3);
        $this->assign('sub5', $sub5);
        $this->assign('sub6', $sub6);
        $this->assign('sub7', $sub7);
        $userlagin = Db::query("SELECT nick,last_login_time FROM hisi_user where DATE_SUB(CURDATE(), INTERVAL 90 DAY) > date(from_unixtime(last_login_time))");
        $this->assign('userlagin',$userlagin);
        $this->assign('userlagincount',count($userlagin));
        $userbir = Db::query("SELECT nick, shop_id,from_unixtime(birthday)
                                FROM hisi_user
                                WHERE date_format( from_unixtime(birthday), '%m%d' )
                                BETWEEN date_format( now( ) , '%m%d' )
                                AND date_format( date_add( now( ) , INTERVAL 7
                                DAY ) , '%m%d' )");
        foreach ($userbir as $key => $value) {
            $userbir[$key]['shop_name'] = Db::name('shop')->where('id',$value['shop_id'])->value('name');
        }
        $this->assign('userbir',$userbir);
        $this->assign('userbircount',count($userbir));
        // halt(cookie('hisi_iframe'));
        if (cookie('hisi_iframe')) {
            $this->view->engine->layout(false);
            return $this->fetch('iframe');
        } else {
            // echo $this->request->controller();die;
            if($this->request->module() == 'system' && $this->request->controller() == 'Index' && $this->request->action() == 'index'){
                $this->view->engine->layout(false);

            }
            return $this->fetch();
        }
    }

    public function getliushi()
    {

        // $userlagin = Db::name('user')->whereTime('last_login_time','-90 days')->field('nick,last_login_time')->select();
        $userlagin = Db::query("SELECT nick,last_login_time FROM hisi_user where DATE_SUB(CURDATE(), INTERVAL 90 DAY) > date(from_unixtime(last_login_time))");

        $this->assign('userlagin',$userlagin);
        $this->assign('userlagincount',count($userlagin));
        $this->view->engine->layout(false);
        return $this->fetch();
    }
    public function getuserbircount()
    {
        $userbir = Db::query("SELECT nick, shop_id,from_unixtime(birthday,'%Y-%m-%d') as birthday1
                                FROM hisi_user
                                WHERE date_format( from_unixtime(birthday), '%m%d' )
                                BETWEEN date_format( now( ) , '%m%d' )
                                AND date_format( date_add( now( ) , INTERVAL 7
                                DAY ) , '%m%d' )");
        foreach ($userbir as $key => $value) {
            $userbir[$key]['shop_name'] = Db::name('shop')->where('id',$value['shop_id'])->value('name');
        }
        $this->assign('userbir',$userbir);
        $this->assign('userbircount',count($userbir));
        $this->view->engine->layout(false);
        return $this->fetch();
    }
    /**
     * 欢迎首页
     * @author
     * @return mixed
     */
    public function welcome()
    {
        $admin_type = session('admin_type');
        // halt($admin_type);
        $this->assign('admin_type',$admin_type);
        $sub0 = Db::name('subscribe')->where('status',0)->where('shop_id',session('shop_id'))->count('id');
        $sub1 = Db::name('subscribe')->where('status',1)->where('shop_id',session('shop_id'))->count('id');
        $sub2 = Db::name('subscribe')->where('status',2)->where('shop_id',session('shop_id'))->count('id');
        $sub3 = Db::name('subscribe')->where('status',3)->where('shop_id',session('shop_id'))->count('id');
        $sub5 = Db::name('subscribe')->where('status',5)->where('shop_id',session('shop_id'))->count('id');
        $sub6 = Db::name('subscribe')->where('status',6)->where('shop_id',session('shop_id'))->count('id');
        $this->assign('sub0', $sub0);
        $this->assign('sub1', $sub1);
        $this->assign('sub2', $sub2);
        $this->assign('sub3', $sub3);
        $this->assign('sub5', $sub5);
        $this->assign('sub6', $sub6);
        $userlagin = Db::query("SELECT nick,last_login_time FROM hisi_user where DATE_SUB(CURDATE(), INTERVAL 90 DAY) > date(from_unixtime(last_login_time))");
        $this->assign('userlagin',$userlagin);
        $this->assign('userlagincount',count($userlagin));
        $userbir = Db::query("SELECT nick, shop_id,from_unixtime(birthday)
                                FROM hisi_user
                                WHERE date_format( from_unixtime(birthday), '%m%d' )
                                BETWEEN date_format( now( ) , '%m%d' )
                                AND date_format( date_add( now( ) , INTERVAL 7
                                DAY ) , '%m%d' )");
        foreach ($userbir as $key => $value) {
            $userbir[$key]['shop_name'] = Db::name('shop')->where('id',$value['shop_id'])->value('name');
        }
        $this->assign('userbir',$userbir);
        $this->assign('userbircount',count($userbir));
        return $this->fetch('index');
    }

    /**
     * 清理缓存
     * @author
     * @return mixed
     */
    public function clear()
    {
        $path   = Env::get('runtime_path');
        $cache  = $this->request->param('cache/d', 0);
        $log    = $this->request->param('log/d', 0);
        $temp   = $this->request->param('temp/d', 0);

        if ($cache == 1) {
            Dir::delDir($path.'cache');
        }

        if ($temp == 1) {
            Dir::delDir($path.'temp');
        }

        if ($log == 1) {
            Dir::delDir($path.'log');
        }

        return $this->success('任务执行成功');
    }
}
